﻿namespace LevelEditors
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;

    public class UIElement : MonoBehaviour, IBeginDragHandler, IEventSystemHandler, IEndDragHandler, IDragHandler, IPointerClickHandler
    {
        [CompilerGenerated]
        private static Func<Renderer, bool> <>f__am$cache16;
        [CompilerGenerated]
        private static Func<Renderer, Bounds> <>f__am$cache17;
        [CompilerGenerated]
        private static Action <>f__am$cache18;
        private Coroutine createPreview;
        public bool isButton;
        private static bool isLoading;
        private float lastClickTime;
        private bool lastValue;
        [SerializeField]
        private ContentSizeFitter m_contentSizeFitter;
        [SerializeField]
        private Text m_labelText;
        private GameObject m_preview;
        private Toggle m_toggle;
        private bool manuallyOn;
        public Action onDoubleClick;
        public Action onLeftClick;
        public Action onReorder;
        public Action onRightClick;
        public Action onSelected;
        private List<RaycastResult> results = new List<RaycastResult>();
        private Canvas rootCanvas;
        private RectTransform rootCanvasTsm;

        private void Awake()
        {
            this.rootCanvas = base.transform.root.GetComponentInChildren<Canvas>();
            this.rootCanvasTsm = this.rootCanvas.GetComponent<RectTransform>();
            if (this.toggle != null)
            {
                this.toggle.onValueChanged.AddListener(delegate (bool value) {
                    if ((value && (this.onSelected != null)) && (!this.manuallyOn && !this.lastValue))
                    {
                        this.onSelected();
                    }
                    this.manuallyOn = false;
                    if ((this.lastValue && value) && (((Time.time - this.lastClickTime) < 0.5f) && (this.onDoubleClick != null)))
                    {
                        this.onDoubleClick();
                    }
                    this.lastValue = value;
                    this.lastClickTime = Time.time;
                    if (this.isButton)
                    {
                        this.isOn = false;
                    }
                });
            }
        }

        private void FinishLoading()
        {
            isLoading = false;
        }

        private UIElement GetTarget(PointerEventData eventData)
        {
            EventSystem.current.RaycastAll(eventData, this.results);
            foreach (RaycastResult result in this.results)
            {
                if (result.gameObject != base.gameObject)
                {
                    UIElement componentInParent = result.gameObject.GetComponentInParent<UIElement>();
                    if (componentInParent != null)
                    {
                        return componentInParent;
                    }
                }
            }
            return null;
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (this.allowReorder)
            {
                this.toggle.interactable = false;
            }
        }

        public void OnDrag(PointerEventData eventData)
        {
            currentDragging = this;
            if (this.allowReorder)
            {
                Camera worldCamera = this.rootCanvas.worldCamera;
                Vector3 end = (worldCamera == null) ? ((Vector3) eventData.position) : worldCamera.ScreenToWorldPoint((Vector3) eventData.position);
                Debug.DrawLine(Vector3.zero, end, Color.red);
                end.x = base.transform.position.x;
                this.tsm.position = end;
                UIElement target = this.GetTarget(eventData);
                if (target != null)
                {
                    int num = (target.transform.position.y <= end.y) ? -1 : 0;
                    base.transform.SetSiblingIndex(target.transform.GetSiblingIndex() + num);
                }
            }
        }

        private void OnEnable()
        {
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (<>f__am$cache18 == null)
            {
                <>f__am$cache18 = (Action) (() => (currentDragging = null));
            }
            this.StartWaitForEndOfFrame(<>f__am$cache18);
            if (this.allowReorder)
            {
                this.onReorder();
                this.toggle.interactable = true;
            }
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if ((eventData.button == PointerEventData.InputButton.Left) && (this.onLeftClick != null))
            {
                this.onLeftClick();
            }
            if ((eventData.button == PointerEventData.InputButton.Right) && (this.onRightClick != null))
            {
                this.onRightClick();
            }
        }

        public void SetPreviewLazy(Func<GameObject> previewGetter)
        {
            <SetPreviewLazy>c__AnonStorey36 storey = new <SetPreviewLazy>c__AnonStorey36 {
                previewGetter = previewGetter,
                <>f__this = this
            };
            this.preview = null;
            if (this.createPreview != null)
            {
                base.StopCoroutine(this.createPreview);
            }
            this.createPreview = this.StartWaitUntil(new Func<bool>(storey.<>m__10), new Action(storey.<>m__11));
        }

        public bool allowReorder
        {
            get
            {
                return (this.onReorder != null);
            }
        }

        public static UIElement currentDragging
        {
            [CompilerGenerated]
            get
            {
                return <currentDragging>k__BackingField;
            }
            [CompilerGenerated]
            private set
            {
                <currentDragging>k__BackingField = value;
            }
        }

        public bool fit
        {
            get
            {
                return this.m_contentSizeFitter.enabled;
            }
            set
            {
                this.m_contentSizeFitter.enabled = value;
            }
        }

        public FontStyle fontStyle
        {
            get
            {
                return this.m_labelText.fontStyle;
            }
            set
            {
                this.m_labelText.fontStyle = value;
            }
        }

        public ToggleGroup group
        {
            get
            {
                return ((this.toggle == null) ? null : this.toggle.group);
            }
            set
            {
                if (this.toggle != null)
                {
                    this.toggle.group = value;
                }
            }
        }

        public string guid { get; set; }

        public int index { get; set; }

        public bool isActive
        {
            get
            {
                return base.gameObject.activeSelf;
            }
            set
            {
                base.gameObject.SetActive(value);
            }
        }

        public bool isOn
        {
            get
            {
                return ((this.toggle != null) && this.toggle.isOn);
            }
            set
            {
                if (this.toggle != null)
                {
                    this.toggle.isOn = value;
                }
                if (value)
                {
                    this.manuallyOn = true;
                }
            }
        }

        private bool isVisible
        {
            get
            {
                return this.rootCanvasTsm.GetWorldRect().Contains(this.tsm.position);
            }
        }

        public string label
        {
            get
            {
                return ((this.m_labelText == null) ? null : this.m_labelText.text);
            }
            set
            {
                if (this.m_labelText != null)
                {
                    this.m_labelText.text = value;
                }
            }
        }

        public TextAnchor labelAlignment
        {
            get
            {
                return ((this.m_labelText == null) ? TextAnchor.MiddleCenter : this.m_labelText.alignment);
            }
            set
            {
                if (this.m_labelText != null)
                {
                    this.m_labelText.alignment = value;
                }
            }
        }

        public Vector2 pivot
        {
            get
            {
                return this.tsm.pivot;
            }
            set
            {
                this.tsm.pivot = value;
            }
        }

        public GameObject preview
        {
            get
            {
                return this.m_preview;
            }
            set
            {
                if (this.m_preview != value)
                {
                    GameObject preview = this.m_preview;
                    if (value != null)
                    {
                        GameObject obj3 = UnityEngine.Object.Instantiate<GameObject>(value);
                        obj3.transform.SetParent(base.transform);
                        if (<>f__am$cache16 == null)
                        {
                            <>f__am$cache16 = x => !(x is ParticleSystemRenderer);
                        }
                        IEnumerable<Renderer> source = obj3.GetComponentsInChildren<Renderer>(true).Where<Renderer>(<>f__am$cache16);
                        Bounds bounds = new Bounds(Vector3.zero, (Vector3) (Vector3.one * 50f));
                        if (source.Count<Renderer>() > 0)
                        {
                            IEnumerator<Renderer> enumerator = source.GetEnumerator();
                            try
                            {
                                while (enumerator.MoveNext())
                                {
                                    Renderer current = enumerator.Current;
                                    current.material.shader = Shader.Find("Unlit/Texture");
                                }
                            }
                            finally
                            {
                                if (enumerator == null)
                                {
                                }
                                enumerator.Dispose();
                            }
                            if (<>f__am$cache17 == null)
                            {
                                <>f__am$cache17 = x => x.bounds;
                            }
                            bounds = source.Select<Renderer, Bounds>(<>f__am$cache17).ToArray<Bounds>().Merge();
                        }
                        obj3.transform.localPosition = new Vector3(0f, -15f, -50f);
                        obj3.transform.localEulerAngles = new Vector3(333f, 302f, 35.66f);
                        obj3.transform.localScale = (Vector3) ((Vector3.one * 50f) * (1.5f / bounds.size.magnitude));
                        this.m_preview = obj3;
                    }
                    if (preview != null)
                    {
                        UnityEngine.Object.Destroy(preview);
                    }
                }
            }
        }

        public object target { get; set; }

        private Toggle toggle
        {
            get
            {
                return ((this.m_toggle == null) ? (this.m_toggle = base.GetComponent<Toggle>()) : this.m_toggle);
            }
        }

        public RectTransform tsm
        {
            get
            {
                return base.GetComponent<RectTransform>();
            }
        }

        [CompilerGenerated]
        private sealed class <SetPreviewLazy>c__AnonStorey36
        {
            internal UIElement <>f__this;
            internal Func<GameObject> previewGetter;

            internal bool <>m__10()
            {
                return (this.<>f__this.isVisible && !UIElement.isLoading);
            }

            internal void <>m__11()
            {
                UIElement.isLoading = true;
                this.<>f__this.preview = this.previewGetter();
                this.<>f__this.Invoke("FinishLoading", 0.01f);
            }
        }
    }
}

